package com.zxh.mall.order.config;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhengxh
 * @version 1.0
 * @date 2024/5/23 - 05 - 23 - 13:47
 * @description com.zxh.mall.order.config
 */
@Configuration
public class MyRocketMQConfiguration {

    //@Value("${rocketmq.nameser}")
    private String nameser;

    private String orderTopic = "orderTopic";

    public void handler(){

    }

    /**
     * 发送延迟消息
     */
    public void sendDelayMsg(String orderSn) throws Exception{
        // 实例化一个生产者来产生延时消息
        DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
        // 设置NameServer的地址
        producer.setNamesrvAddr(nameser);
        // 启动生产者
        producer.start();
        int totalMessagesToSend = 5;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message message = new Message(orderTopic, orderSn.getBytes());
            // 设置延时等级3,这个消息将在30s之后发送(现在只支持固定的几个时间,详看delayTimeLevel)
            message.setDelayTimeLevel(4);
            // 发送消息
            producer.send(message);
        }
        // 关闭生产者
        producer.shutdown();
    }
}
